package com.freesky.provider.order.controller;

import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.pojo.Result;
import com.freesky.common.utils.ResultUtil;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.common.vo.VOItem;
import com.freesky.common.vo.VOProductDet;
import com.freesky.common.dao.entity.Item;
import com.freesky.provider.order.service.IItemService;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.ApiOperation;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.jms.Destination;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 10:06 2018/12/14
 * @description:
 */
@RestController
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping(value = "/item/{itemId}")
    public JSONResponse getItemById(@PathVariable(value = "itemId") String itemId){

        return itemService.getItemById(itemId);
    }

    @GetMapping(value = "/item/list")
    public DataTablesResult getItemList(
            @RequestParam(value = "draw") int draw,
            @RequestParam(value = "start") int start,
            @RequestParam(value = "length") int length,
            @RequestParam(value = "cid") int cid,
            @RequestParam(value = "search[value]") String search,
            @RequestParam(value = "order[0][column]") int orderCol,
            @RequestParam(value = "order[0][dir]") String orderDir){


        //获取客户端需要排序的列
        String[] cols = {"checkbox","id", "image", "title", "sell_point", "price", "created", "updated", "status"};
        String orderColumn = cols[orderCol];
        if(orderColumn == null) {
            orderColumn = "created";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        return itemService.getItemList(draw,start,length,cid,search,orderColumn,orderDir);
    }

    @GetMapping(value = "/item/listSearch")
    public DataTablesResult getItemSearchList(
            int draw,
            int start,
            int length,
            int cid,
            String searchKey,
            String minDate,
            String maxDate,
            @RequestParam(value = "search[value]") String search,
            @RequestParam(value = "order[0][column]") int orderCol,
            @RequestParam(value = "order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","id", "image", "title", "sell_point", "price", "created", "updated", "status"};
        //默认排序列
        String orderColumn = cols[orderCol];
        if(orderColumn == null) {
            orderColumn = "created";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if(!search.isEmpty()){
            searchKey=search;
        }
        DataTablesResult result=itemService.getItemSearchList(draw,start,length,cid,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }

    @GetMapping(value = "/item/count")
    public DataTablesResult getAllItemCount(){

        DataTablesResult result=itemService.getAllItemCount();
        return result;
    }

    @PutMapping(value = "/item/stop/{id}")
    public JSONResponse stopItem(@PathVariable(value = "id") String id){

        Item item = itemService.alertItemState(id,0);
        return JSONResponseDiretor.buildSuccessJSONResponse(item);
    }

    @PutMapping(value = "/item/start/{id}")
    public JSONResponse startItem(@PathVariable(value = "id") String id){

        Item item = itemService.alertItemState(id,1);
        return JSONResponseDiretor.buildSuccessJSONResponse(item);
    }

    @DeleteMapping(value = "/item/del/{ids}")
    public JSONResponse deleteItem(@PathVariable(value = "ids") String[] ids){
        return itemService.deleteItem(ids);
    }

    @PostMapping(value = "/item/add")
    public JSONResponse addItem(@RequestBody VOItem voItem){

        return itemService.addItem(voItem);
    }

    @PostMapping(value = "/item/update/{id}")
    public JSONResponse updateItem(@PathVariable(value = "id") String id,@RequestBody VOItem voItem){

        return itemService.updateItem(id,voItem);
    }

    @GetMapping(value = "/item/detail")
    public JSONResponse getItemDetail(@RequestParam(value = "productId")String id) {
        return itemService.getItemDetail(id);
    }


    @GetMapping(value = "/item/get_all")
    public JSONResponse getAllProduct(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "20") int size,
            @RequestParam(value = "sort", defaultValue = "") String sort,
            @RequestParam(value = "cid", defaultValue = "") Long cid,
            @RequestParam(value = "priceGt", defaultValue = "-1") int priceGt,
            @RequestParam(value = "priceLte", defaultValue = "-1") int priceLte) {

        return itemService.getAll(page, size, sort, cid, priceGt, priceLte);
    }
}
